<?php

/**
 * 产品管理
 * @author 陈德本
 *
 */
class productService extends commonService
{

    /**
     * 产品主表
     *
     * @var productDao
     */
    protected $pao;

    /**
     * 产品分类
     *
     * @var pro_catDao
     */
    protected $pro_cat;

    /**
     * 产品属性
     *
     * @var pro_attrDao
     */
    protected $pro_attr;

    public function __construct()
    {
        parent::__construct();
        $this->pao = InitPHP::getDao("product");
        $this->pro_attr = InitPHP::getDao("pro_attr");
        $this->pro_cat = InitPHP::getDao("pro_cat");
    }

    /**
     * 获取产品信息；$id=null则表示获取全部记录
     * 
     * @param string $id            
     * @return Ambigous <multitype:, boolean, multitype:multitype: >
     */
    public function getProduct($id = NULL)
    {
        $map = array();
        if ($id) {
            $map = array(
                'pid' => $id
            );
        }
        return $this->pao->getAllByMutiple($map,array(), array(
            'id' => 'desc'
        ));
    }

    /**
     * 获取推荐产品
     * 
     * @return Ambigous <multitype:, boolean, multitype:multitype: >
     */
    public function getTjPro($num=8)
    {
        $map = array(
            'is_recommand' => 1
        );
        return $this->pao->getAllByMutiple($map, array(), array('id'=>'desc'), $num);
    }

    public function add_attr($id, $attr_key, $attr_value)
    {
        $this->pro_attr->deleteByField(array(
            'pid' => $id
        ));
        $data = array();
        foreach ($attr_key as $k => $v) {
            $temp = array();
            $temp['pid'] = $id;
            $temp['attr_attr'] = $v['attr'];
            $temp['attr_name'] = $attr_value[$k]['attr_value'];
            $temp['sort'] = $k;
            $this->pro_attr->add($temp);
        }
        return true;
    }

    public function getAttr($id)
    {
        $map = array(
            'pid' => $id
        );
        return $this->pro_attr->getAllByMutiple($map, array(), array(
            'sort' => 'asc'
        ));
    }

    /**
     * 获取产品分类
     *
     * @param string $tree
     *            是否返回Tree
     * @return Ambigous <multitype:, multitype:array , boolean, multitype:multitype: >
     */
    public function getMenu($tree = TRUE)
    {
        $data = $this->pro_cat->getAllByMutiple(array(), array(), array(
            'sort' => 'asc'
        ));
        if ($tree) {
            $data = list_to_tree($data, "id", "pid", "products");
        }
        return $data;
    }

    /**
     * 获取分类数组
     */
    public function getCatTree($flag = TRUE)
    {
        $data = $this->pro_cat->getAllByMutiple(array(), array(), array(
            'sort' => 'asc'
        ));
        if (! $flag) {
            foreach ($data as $k => $v) {
                $result[$v['id']] = $v['title'];
            }
        } else {
            $data = list_to_tree($data, "id", "pid", "products");
            $result = array();
            if ($data) {
                foreach ($data as $k => $v) {
                    $result[$v['id']] = $v['title'];
                    if ($v['products']) {
                        foreach ($v['products'] as $k2 => $v2) {
                            $result[$v2['id']] = "&nbsp;&nbsp;|--&nbsp;&nbsp;" . $v2['title'];
                        }
                    }
                }
            }
        }
        return $result;
    }
    /**
     * 获取分类数组2
     */
    public function getCatTree2($flag = TRUE)
    {
        $data = $this->pro_cat->getAllByMutiple(array(), array(), array(
            'sort' => 'asc'
        ));
        if (! $flag) {
            foreach ($data as $k => $v) {
                $result[$v['id']]['title'] = $v['title'];
                $result[$v['id']]['url'] = $v['url'];
            }
        } else {
            $data = list_to_tree($data, "id", "pid", "products");
            $result = array();
            if ($data) {
                foreach ($data as $k => $v) {
                    $result[$v['id']] = $v['title'];
                    if ($v['products']) {
                        foreach ($v['products'] as $k2 => $v2) {
                            $result[$v2['id']] = "&nbsp;&nbsp;|--&nbsp;&nbsp;" . $v2['title'];
                        }
                    }
                }
            }
        }
        return $result;
    }

    /**
     * 获取一条产品分类记录
     *
     * @param int $id            
     */
    public function getOneCat($id)
    {
        return $this->pro_cat->getOne($id);
    }

    /**
     * 添加一条产品分类
     *
     * @param array $data            
     */
    public function add_cat($data)
    {
        return $this->pro_cat->add($data);
    }

    /**
     * 删除一条产品分类
     *
     * @param int $id            
     * @return boolean
     */
    public function delete_cat($id)
    {
        return $this->pro_cat->delete($id);
    }

    /**
     * 更新一条产品分类记录
     *
     * @param int $id            
     * @param array $data            
     * @return boolean
     */
    public function update_cat($id, $data)
    {
        return $this->pro_cat->update($id, $data);
    }

    /**
     * 获取分类列表
     * 
     * @param int $pid
     *            分类父ID
     * @param int $start
     *            开始的记录数
     * @param int $pernum
     *            每页条数
     * @return multitype:unknown number
     */
    public function getList($pid, $start, $pernum)
    {
        $result = $this->pao->getPro($pid, $pernum, $start, 'id', 'asc');
        foreach ($result[0] as $k => $v) {
            if ($v['has_attr']) {
                $result[0][$k]['attr'] = $this->getAttr($v['id']);
            }
        }
        return $result;
    }
}